using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;

using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using BrainTechLLC;

namespace Mud.EmlenMudMain
{
    [Serializable]
    public class RaceData : GameThingBaseClass
    {
        public RaceData() { }
        private int[] bonus = new int[5];	/* Initial stat bonuses */

        public int[] Bonus
        {
            get { return bonus; }
            set { bonus = value; }
        }
        private int[] limits = new int[5];	/* Stat limits */

        public int[] Limits
        {
            get { return limits; }
            set { limits = value; }
        }
        private int start_room;	/* Starting currentRoom, 99 or 100.. 99=evil 100=good */

        public int Start_room
        {
            get { return start_room; }
            set { start_room = value; }
        }
        private int max_skills;	/* Max skills */

        public int Max_skills
        {
            get { return max_skills; }
            set { max_skills = value; }
        }
        private int max_spells;	/* Max spells */

        public int Max_spells
        {
            get { return max_spells; }
            set { max_spells = value; }
        }
        private int height_min;	/* Minimum height */

        public int Height_min
        {
            get { return height_min; }
            set { height_min = value; }
        }
        private int height_max;	/* Maximum height */

        public int Height_max
        {
            get { return height_max; }
            set { height_max = value; }
        }
        private int effect_bits;	/* Natural abilities */

        public int Effect_bits
        {
            get { return effect_bits; }
            set { effect_bits = value; }
        }
        private int natarmor;	/* Natural skin naturalArmor class */

        public int Natarmor
        {
            get { return natarmor; }
            set { natarmor = value; }
        }
        private int regen_hit_percent; /* Hit point regeneration percent */

        public int Regen_hit_percent
        {
            get { return regen_hit_percent; }
            set { regen_hit_percent = value; }
        }
        private int regen_move_percent; /* Move point regenration percent */

        public int Regen_move_percent
        {
            get { return regen_move_percent; }
            set { regen_move_percent = value; }
        }
        private bool poison_immunity; /* Is this race immune to poison? */

        public bool Poison_immunity
        {
            get { return poison_immunity; }
            set { poison_immunity = value; }
        }
        private bool plague_immunity; /* Is this race immune to plague? */

        public bool Plague_immunity
        {
            get { return plague_immunity; }
            set { plague_immunity = value; }
        }
        private bool switch_aligns;   /* Can this race switch alignment? */

        public bool Switch_aligns
        {
            get { return switch_aligns; }
            set { switch_aligns = value; }
        }

        public static RaceData GetRaceData(string racename)
        {
            if (racename == null) return null;
            for (int i = 0; i < EmlenMud.WorldTemplates.AllRaces.Count; i++)
            {
                RaceData race = (RaceData)EmlenMud.WorldTemplates.AllRaces[i];
                if (race.ShortName == racename) { return race; }
            }
            return null;
        }


        public void readOldRaceInfo()
        {
            char[] spc = new char[1];
            spc[0] = ' ';
            FileStream fs = null;
            try
            {
                fs = new FileStream(EmlenMud.HostPathnameToMudDirectory + "\\races.dat", FileMode.Open, FileAccess.Read);
            }
            catch (IOException ex) { EmlenMud.LogError("Error reading races.dat: ", ex, LogType.Always); return; }

            StreamReader sr = new StreamReader(fs);
            string szLine = sr.ReadLine().Trim();

            while (szLine != "END" && sr.EndOfStream == false)
            {
                if (szLine[0] != '#')
                {
                    string[] rgsz = szLine.Split(spc, StringSplitOptions.RemoveEmptyEntries);
                    if (rgsz.Length > 21)
                    {
                        try
                        {
                            RaceData race = new RaceData();
                            race.Name = new NameAndDescription() { Name = rgsz[0], FriendlyUniqueName = rgsz[0] };
                            race.bonus[0] = Convert.ToInt32(rgsz[1]);
                            race.bonus[1] = Convert.ToInt32(rgsz[2]);
                            race.bonus[2] = Convert.ToInt32(rgsz[3]);
                            race.bonus[3] = Convert.ToInt32(rgsz[4]);
                            race.bonus[4] = Convert.ToInt32(rgsz[5]);
                            race.limits[0] = Convert.ToInt32(rgsz[6]);
                            race.limits[1] = Convert.ToInt32(rgsz[7]);
                            race.limits[2] = Convert.ToInt32(rgsz[8]);
                            race.limits[3] = Convert.ToInt32(rgsz[9]);
                            race.limits[4] = Convert.ToInt32(rgsz[10]);
                            race.start_room = Convert.ToInt32(rgsz[11]);
                            race.max_skills = Convert.ToInt32(rgsz[12]);
                            race.max_spells = Convert.ToInt32(rgsz[13]);
                            race.height_min = Convert.ToInt32(rgsz[14]);
                            race.height_max = Convert.ToInt32(rgsz[15]);
                            race.effect_bits = Convert.ToInt32(rgsz[16]);
                            race.natarmor = Convert.ToInt32(rgsz[17]);
                            race.regen_hit_percent = Convert.ToInt32(rgsz[18]);
                            race.regen_move_percent = Convert.ToInt32(rgsz[19]);
                            race.poison_immunity = Convert.ToInt32(rgsz[20]) == 1 ? true : false;
                            race.plague_immunity = Convert.ToInt32(rgsz[21]) == 1 ? true : false;
                            race.switch_aligns = Convert.ToInt32(rgsz[22]) == 1 ? true : false;
                            EmlenMud.WorldTemplates.AllRaces.Add(race);
                        }
                        catch (Exception ex)
                        {
                            EmlenMud.LogError("Races.dat, line is incorrect: " + szLine, ex, LogType.Always);
                        }
                    }
                    else
                    {
                        EmlenMud.LogError("Races.dat, line is incorrect: " + szLine, null, LogType.Always);
                    }
                }
            }
            sr.Close();
        }
    }
}
